package com.dataseek.iot.sys.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.sys.entity.Menu;
import com.dataseek.iot.sys.service.DbService;
import com.dataseek.iot.sys.service.MenuService;
import com.dataseek.iot.sys.vo.MenuVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import com.dataseek.iot.toolikit.common.util.CustomBeanUtils;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private DbService dbService;

    /**
     * 查询菜单列表
     *
     * @return
     */

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult querymenu() throws IOException {
        List<Menu> menuList = menuService.queryAllMenuTree();
        JSONArray array = null;
        if(menuList!=null&&menuList.size()>0){
            array= JSONArray.parseArray(JSON.toJSONString(menuList));
            array= CustomBeanUtils.allLevelSort(array,"orderNum","children",false);
        }
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",array);
    }

    /**
     * 修改菜单列表
     *
     * @param menu 菜单实体类
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult updatemenu(@RequestBody Menu menu) throws IOException {
        Integer code  = menuService.updateMenu(menu);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

    /**
     * 新建菜单
     *
     * @param menu 菜单实体类
     * @return
     */

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insertmenu(@RequestBody Menu menu) throws IOException {
        Integer code  = menuService.insertMenu(menu);

        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult=new ResponseResult(RspStatusEnum.FAIL.getCode(),"菜单路径重复，请重新输入!",code.toString());
        } else {
            responseResult=new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!",code.toString());
        }
        return responseResult;
    }


    /**
     * 删除菜单
     *
     * @param menuIds 菜单ID,满足批量删除
     * @return
     */

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult deletemenu(@RequestBody JSONObject menuIds)  throws IOException {
        JSONArray jsonArray = menuIds.getJSONArray("ids");
        List<Integer> ids = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        Integer code  = menuService.deleteMenu(ids);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }





}
